package com.yijiu.trb;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yijiu.action.BaseAction;
import com.yijiu.adapter.InvestorListAdapter;
import com.yijiu.adapter.ScrollFootListener;
import com.yijiu.network.Request;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.resource.InvestorList;
import com.yijiu.response.RequestInvestorRecordResponse;

/**
 * 投资人管理
 * 
 * @author TangChao
 */
public class InvesterManagerActivity extends BTitleActivity implements ScrollFootListener, OnItemClickListener
{
	private ListView listView;

	private InvestorListAdapter adapter;

	private int pageIndex = 1;

	private boolean isLoadOver, onFoot = false;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.invester_manager_layout);
		initWidget();
		RequestInvestorRecord(pageIndex, onFoot);
	}

	@Override
	protected void initWidget()
	{
		super.initWidget();
		titleView.setText("投资人管理");
		listView = (ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(this);
	}

	/**
	 * 客户管理信息请求
	 */
	private void RequestInvestorRecord(int pageIndex, boolean onFoot)
	{
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=appInvestorRecordService&pageNo=" + pageIndex + "&pageSize=10";
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction action = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		RequestInvestorRecordResponse requestInvestorRecordResponse = new RequestInvestorRecordResponse();
		// 请求服务器
		Request request = new Request(action, requestInvestorRecordResponse, ActionType.REQUEST_INVESTOR_RECORD);
		netManager.excute(request, this, this);
		if (onFoot)
		{
			adapter.showprogress();
		}
		else
		{
			showProgress("正在获取投资人数据，请稍候");
		}
	}

	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_INVESTOR_RECORD:
				if (onFoot)
				{
					adapter.hideprogress();
				}
				RequestInvestorRecordResponse requestInvestorRecordResponse = (RequestInvestorRecordResponse) r.getResponse();
				if (requestInvestorRecordResponse.investorList == null || requestInvestorRecordResponse.investorList.isEmpty()
						|| requestInvestorRecordResponse.investorList.size() < 10)
				{
					isLoadOver = true;
				}
				else
				{
					isLoadOver = false;
				}
				if (adapter == null || pageIndex == 1)
				{
					adapter = new InvestorListAdapter(requestInvestorRecordResponse.investorList, this);
					adapter.setScrollFootListener(this);
					listView.setAdapter(adapter);
				}
				else
				{
					if (requestInvestorRecordResponse.investorList != null
							&& !requestInvestorRecordResponse.investorList.isEmpty())
					{
						adapter.addAll(requestInvestorRecordResponse.investorList);
					}
				}
				break;
			default:
				break;
		}
	}

	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		app.showMsg(errorMsg);
	}

	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	{
		InvestorList item = (InvestorList) arg0.getItemAtPosition(arg2);
		Intent intent = new Intent(this, InvestorDetailActivity.class);
		intent.putExtra("userBaseId", item.userBaseId);
		startActivity(intent);
	}

	@Override
	public void onFoot()
	{
		if (isLoadOver)
		{
			return;
		}
		onFoot = true;
		RequestInvestorRecord(++pageIndex, onFoot);
	}
}
